#ifndef __ERROR_CODES_TABLE_VIEW_H__
#define __ERROR_CODES_TABLE_VIEW_H__

#include <QWidget>
#include <QTableWidget>
#include <QSqlTableModel>
#include <QSqlDatabase>
#include <QSqlRelationalTableModel>
#include <QString>
#include "module.h"
#include "cellwidget.h"
#include "errorcode.h"
#include "dbq_getallerrors.h"
#include "dbq_geterrorbyid.h"

class ErrorCodesTableView : public QTableWidget
{
    Q_OBJECT

  enum {
    COLUMN_ID_MODULE_NAME = 0,
    COLUMN_ID_ERROR_CODE_ID,
    COLUMN_ID_ERROR_CODE_SHORT_DESCRIPTION,
    COLUMN_ID_ERROR_CODE_FULL_DESCRIPTION,
    COLUMN_MAX
  };

  const static QString COLUMN_NAME[COLUMN_MAX];
  const static int     COLUMN_WIDTH_PREFERED[COLUMN_MAX];

public:
    explicit ErrorCodesTableView(QString table_name, QWidget *parent = 0, QSqlDatabase db = QSqlDatabase());

  void clear();

protected:
  CellWidget* getNewCellWidgetInstance();
  void        setCellString(int row, int column, const QString &data);
  QString     getCellString(int row, int column) const;
  QString     text(int row, int column) const;
  CellWidget *getCell(int row, int column) const;

  Errorcode   getErrorCode(const long &errorid, bool offline = false);
  QString     getErrorCodeDescription(const long &errorid);
  Module      getModule(int rowID);
  long        getErrorID(int rowID);

public:
  void        addErrorcode(Errorcode, int row = -1);

signals:
    void setErrorCode(const Errorcode &errorcode);

public slots:
    virtual void updateTable(const Module &);
    virtual void updateSectionWidth(int logicalIndex,int, int newSize);
    virtual void updateSectionHeight(int logicalIndex, int, int newSize);
    virtual void verticalHeaderSectionClicked(int);
    virtual void cellClicked ( int row, int column );


protected:
  QSqlRelationalTableModel *pSqlTabModel;
  static const int          minimumTableWidth;

  static mysqlquery::dbq_getallerrors db_query_get_all_errors;
  static mysqlquery::dbq_geterror4module db_query_get_errors_4_module;
  static mysqlquery::dbq_geterrorbyid db_query_get_error_by_id;
};

#endif // __ERROR_CODES_TABLE_VIEW_H__
